<style scoped>
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }



</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>修改部门</legend>
        </fieldset>

        <form class="layui-form " >
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" :value="adata.name" lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属公司</label>
                <div class="layui-input-inline">
                    <select name="orgguid" id="orgguid"  lay-verify="required" lay-filter="orgguid"  lay-search="">
                        <option value="">请选择所属公司</option>

                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">上级部门</label>
                <div class="layui-input-block"  >
                    <select name="upGuid">
                        <option value="">请选择上级部门</option>
                        <option v-for="(item, index) in departments" :selected="item.code == adata.upGuid" :value="item.code">{{item.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">部门编码: </label>
                <div class="layui-input-block">
                    <input type="text" name="code" v-model="dcode" @blur="checkCode" lay-verify="required" placeholder="请输入部门编码" class="layui-input">
                </div>

            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">部门类型  </label>
                <div class="layui-input-inline">
                    <select name="type" id="type" lay-verify="required" v-model="adata.type"  lay-search >
                        <option value="">请选择类型</option>
                        <option v-for="(item, index) in dict" :key="item.dictkey" :value="item.dictkey">{{item.dicttext}}</option>
                    </select>
                </div>
            </div>
            <!-- <div class="layui-form-item">
                <label class="layui-form-label">电商部门</label>
                <div class="layui-input-block">
                    <input type="radio" name="flag" value="1" title="是" :checked="adata.flag == 1">
                    <input type="radio" name="flag" value="2" title="否" :checked="adata.flag == 2">
                </div>
            </div> -->

            <div class="layui-form-item">
                <label class="layui-form-label">负责人</label>
                <div class="layui-input-block">
                    <input type="text" name="manager" :value="adata.manager" placeholder="请输入负责人" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">负责人电话</label>
                <div class="layui-input-block">
                    <input type="text" name="managertel" :value="adata.managertel" placeholder="请输入负责人电话" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否计划部门</label>
                <div class="layui-input-block">
                   <input type="radio" name="flag" value="1" title="是" :checked="adata.flag == 1">
                    <input type="radio" name="flag" value="0" title="否" :checked="adata.flag == 0">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <textarea name="description" :value="adata.description" placeholder="请输入描述内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="remark" :value="adata.remark" placeholder="请输入备注内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'EditDepartment',
        props: ['id'],
        data () {
            return {
                 guid : this.id,
                 adata: {},
                dict:[],
                departments: [],
                dcode: ''
            }
        },
        mounted() {
            let that = this;
            that.getTypeEmnu();
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                let loader = layer.load();
                //获取回显数据
                $.ajax({
                    url: '/sales/Department!get.action',
                    data: {guid: that.guid},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != null) {
                            that.adata = res.info;
                            that.dcode = that.substr3(that.adata.code);
                            that.loadOrganization(that,res.info.orgguid);
                            that.loadDepart(res.info.orgguid);
                            //that.checkIsUpdate(res.info.code);
                            form.render('select');
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });

                //提交编辑过的数据
                form.on('submit(submit)', function (data) {

                    let loader = layer.load();
                    data.field.guid = that.guid;
                    $.ajax({
                        url: '/sales/Department!update.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "ok") {
                                that.toast("修改成功");
                                that.$parent.switchPage('Department');
                            } else if (res.info == 'coderep') {
                                that.toastText('该编码部门已存在');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });

                form.on('select(orgguid)', function (data) {
                    var message = $("select[name=orgguid]").val();
                    that.loadDepart(message);

                });
            });

        },
        updated: function () {
            layui.form.render('select');
            layui.form.render();

        },
        methods: {
            checkCode: function() {
                if (this.dcode.length > 3) {
                    this.toastText('部门编码必须是3位');
                    this.dcode = '';
                }
            },
            substr3: function(val) {
                if (val == undefined) return;
                return val.substr(val.length-3,3);
            },
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            },
            loadOrganization: function (that,data) {

                $.ajax({
                    url: '/sales/Organization!listAll.action',
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != 'error' ) {
                            $.each(res.info, function (index, item) {
                                if(that.adata.orgguid == item.guid){
                                    $("#orgguid").append("<option value='"+ item.guid+"' selected>"+item.name+"</option>");

                                }else{
                                    $("#orgguid").append("<option value='"+ item.guid+"'>"+item.name+"</option>");
                                }
                                                           });
                            layui.form.render("select");
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {

                    }
                });
            },
            loadDepart(orgguid){
                var that = this;
                $.ajax({
                    url: '/sales/Department!listAll.action',
                    type: "post",
                    dataType: "json",
                    data:{orgguid:orgguid},
                    success: function(res) {
                        if (res.info != 'error' ) {
                            that.departments = res.info;
                            layui.form.render("select");
                        } else {
                            that.toast("获取部门数据失败");
                        }
                    }
                });
            },
            checkIsUpdate(code){
                $.ajax({
                    url: '/sales/Department!checkIsUpdate.action',
                    type: "post",
                    dataType: "json",
                    async:false,
                    data:{likeCode:code},
                    success: function(res) {
                        if(res.resultCode != 'success'){
                            $('#code2').attr("disabled","true");
                            $('#code1').attr("disabled","true")
                        }

                    },
                    complete: function () {

                    }
                });
            },
            getTypeEmnu: function() {
                this.$post('/sales/Department!getTypeEmnu.action')
                    .then(response => {
                        this.dict = response.data.info;
                    });
            },
        }
    }
</script>
